import { legacy_createStore as createStore, applyMiddleware } from 'redux';
import rootReducer from './reducer/index';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';

const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(thunk)));
export default store;

export type RootState = ReturnType<typeof store.getState>;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;



// 推断出类型: {posts: PostsState, comments: CommentsState, users: UsersState}
// 💥临时解决办法:  https://github.com/reduxjs/redux-thunk/issues/333
export type AppDispatch = typeof store.dispatch
export const useAppDispatch: () => AppDispatch | any = useDispatch